package com.business_clean.app.weight.dialog;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.business_clean.R;
import com.business_clean.app.callback.OnDialogSelectListener;
import com.business_clean.data.mode.group.GroupMangerListEntity;
import com.business_clean.ui.adapter.attendance.CustomMenuAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lxj.xpopup.impl.PartShadowPopupView;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class CustomAttendancePartShadowPopup extends PartShadowPopupView {

    private Context mContext;
    private CustomMenuAdapter mAdapter;

    private OnDialogSelectListener selectListener;

    private List<GroupMangerListEntity> datas;

    private int selectedIndex = -1;

    public CustomAttendancePartShadowPopup(@NonNull Context context, List<GroupMangerListEntity> strings, OnDialogSelectListener selectListener) {
        super(context);
        mContext = context;
        this.selectListener = selectListener;
        this.datas = strings;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_attendance_part_shadow_popup;
    }

    private RecyclerView recyclerView;

    private TextView butReset;
    private TextView butOK;

    @Override
    protected void onCreate() {
        super.onCreate();
        butOK = findViewById(R.id.but_bottom_but_next);
        butReset = findViewById(R.id.but_bottom_but_last);
        recyclerView = findViewById(R.id.recycler_dialog_attendance);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new CustomMenuAdapter();
        recyclerView.setAdapter(mAdapter);
        mAdapter.setList(datas);

        butOK.setText("确定");
        butReset.setText("重置");
        initClick();
    }


    private void initClick() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                mAdapter.updateItem(mAdapter.getData().get(position).getUuid());
                selectedIndex = position;
            }
        });
        butReset.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.updateItem("");
                if (selectListener != null && mAdapter != null) {
                    selectListener.onSelect(-1, "全部");
                }
                dismiss();
            }
        });

        butOK.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectListener != null && mAdapter != null) {
                    selectListener.onSelect(selectedIndex, mAdapter.getIsClick().toString());
                }
                dismiss();
            }
        });

    }

    @Override
    protected void onShow() {
        super.onShow();
        Log.e("tag", "CustomPartShadowPopupView onShow");
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        Log.e("tag", "CustomPartShadowPopupView onDismiss");
    }
}

